﻿using System;
using System.Collections.Generic;

namespace WebStudio.Mvc.MvcPager
{
    /// <summary>
    /// 分页列表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PagedList<T> : List<T>
    {
        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="items"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        public PagedList(IList<T> items, int pageIndex, int pageSize)
        {
            PageSize = pageSize;
            TotalItemCount = items.Count;
            TotalPageCount = (int)Math.Ceiling(TotalItemCount / (double)PageSize);
            if (TotalPageCount == 0)
                TotalPageCount = 1;
            CurrentPageIndex = pageIndex;
            StartRecordIndex = pageIndex * PageSize;
            EndRecordIndex = TotalItemCount > (pageIndex + 1) * pageSize ? (pageIndex + 1) * pageSize - 1 : TotalItemCount - 1;
            for (int i = StartRecordIndex; i <= EndRecordIndex; i++)
            {
                Add(items[i]);
            }
        }
        #endregion

        #region 自定义分页构造函数
        /// <summary>
        /// 自定义分页构造函数
        /// </summary>
        /// <param name="items"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalItemCount"></param>
        public PagedList(IEnumerable<T> items, int pageIndex, int pageSize, int totalItemCount)
        {
            AddRange(items);
            TotalItemCount = totalItemCount;
            TotalPageCount = (int)Math.Ceiling(totalItemCount / (double)pageSize);
            if (TotalPageCount == 0)
                TotalPageCount = 1;
            CurrentPageIndex = pageIndex;
            PageSize = pageSize;
            StartRecordIndex = pageIndex * pageSize;
            EndRecordIndex = TotalItemCount > (pageIndex + 1) * pageSize ? (pageIndex + 1) * pageSize - 1 : totalItemCount - 1;
        }
        #endregion


        //属性

        /// <summary>
        /// 当前页索引(从0开始)
        /// </summary>
        public int CurrentPageIndex { get; set; }

        /// <summary>
        /// 页大小
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalItemCount { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPageCount { get; private set; }

        /// <summary>
        /// 开始记录索引(从0开始)
        /// </summary>
        public int StartRecordIndex { get; private set; }

        /// <summary>
        /// 结束索引
        /// </summary>
        public int EndRecordIndex { get; private set; }
    }
}
